package com.springboot.common.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springboot.core.log.LoggerUtil;
import com.springboot.core.web.mvc.R;

/**
 * 全局异常
 * 
 * @ClassName: GlobalExceptionHandler
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 252956
 * @date 2019年11月11日 下午1:28:23
 *
 */
@ControllerAdvice
class GlobalExceptionHandler {

	// json exceptin
	@ExceptionHandler
	@ResponseBody
	public R jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		R result = new R();
		if (e instanceof NotLoginException) { // 如果是未登录异常
			result.setCode(R.CODE_501);
			result.setMsg(e.getMessage());
		} else if (e instanceof NotPermissionException) { // 如果是权限异常
			result.setCode(R.CODE_502);
			result.setMsg(e.getMessage());
		} else if (e instanceof RequestLimitException) { // 请求限制异常
			result.setCode(R.CODE_504);
			result.setMsg(e.getMessage());
		} else if (e instanceof SystemException) {
			result.setCode(R.CODE_500);
			result.setMsg(e.getMessage());
		} else {
			result.setCode(R.CODE_500);
			result.setMsg("系统异常,请稍后再试！");
			LoggerUtil.error(e.getMessage());
		}

		return result;
	}
}